package com.yunxi.service.dubboService.impl.sat;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.sat.SatCustomerClue;
import com.yunxi.model.sat.SatMess;
import com.yunxi.qrtz.service.CustomerClueRemindJobDubboService;
import com.yunxi.sat.pojo.SatMessAddVO;
import com.yunxi.service.localService.sat.SatCustomerClueService;
import com.yunxi.service.localService.sat.SatMessService;
import com.yunxi.sys.pojo.StaffVO;
import com.yunxi.sys.service.StaffDubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 客户线索超期提醒 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-08-21
 */

public class CustomerClueRemindJobDubboServiceImpl implements CustomerClueRemindJobDubboService {

    private static final Logger logger = LoggerFactory.getLogger(CustomerClueRemindJobDubboServiceImpl.class);

    @Autowired
    @Qualifier(value = "satCustomerClueServiceImpl")
    SatCustomerClueService satCustomerClueService;

    @Autowired
    @Qualifier(value = "staffDubboServiceBiz")
    StaffDubboService staffDubboService;

    @Autowired
    @Qualifier(value = "satMessServiceImpl")
    SatMessService satMessService;


    /**
     * 超期线索生成消息提醒
     * @throws Exception
     */
    @Override
    public void remindCustomerClue() throws Exception {
        //1.查询超期线索
        //20190904 调整超期时间为半小时固定值
        List<SatCustomerClue> list = satCustomerClueService.queryLateClue();
        if(list != null && list.size() > 0){
            for(SatCustomerClue satCustomerClue : list){
                //2.更改线索是否提醒为是
                satCustomerClue.setIsRemind(1);
//                satCustomerClue.setIsGet(2);//超时
                satCustomerClueService.saveOrUpdate(satCustomerClue);
                //3.生成消息给该渠道商下所有销售经理
                SatMess satMess = new SatMess();
                LoginInfo loginInfo = new LoginInfo();
                loginInfo.setPartnerCode(satCustomerClue.getPartnerCode());
                ApiResult rolePartnerSaleManager = staffDubboService.listStaffByRoleCode("rolePartnerSaleManager", loginInfo);
                List<StaffVO> list1= (List<StaffVO>) rolePartnerSaleManager.getData();
                if(list1 != null && list1.size() > 0){
                    for (StaffVO vo : list1){
                        SatMessAddVO satMessAddVO = new SatMessAddVO();
                        satMessAddVO.setType("1");
                        satMessAddVO.setTemplateType(1);
                        satMessAddVO.setCustomerName(satCustomerClue.getCustomerName());
                        satMessAddVO.setCustomerNo(satCustomerClue.getCustomerNo());
                        satMessAddVO.setSalesConsultantNo(vo.getAccountNo());
                        satMessAddVO.setReceiveRoleCode("rolePartnerSaleManager");
                        satMessService.newMess(satMessAddVO);
                    }
                }
            }
        }
        //2.查询销售经理超期线索,超时一个小时,合计距大区经理分配时间一个小时
        //20190904 调整销售经理可查看超期1小时内的线索,过了则流向小区经理
        List<SatCustomerClue> mList = satCustomerClueService.queryManagerLateClue();
        logger.info("====================线索超期一个小时数量:"+mList.size()+"=======================");
        if(mList != null && mList.size() > 0){
            for (SatCustomerClue satCustomerClue : mList){
                String partnerCode = satCustomerClue.getPartnerCode();
                //清空渠道商代码,重置是否提醒为否 0
//                satCustomerClue.setPartnerCode(null);
//                satCustomerClue.setIsRemind(0);
//                satCustomerClue.setIsGet(0);
//                satCustomerClue.setIsRemindManager(1);
//                satCustomerClueService.saveOrUpdate(satCustomerClue);
                logger.info("====================开始清空线索渠道信息=======================");
                satCustomerClueService.returnAreaManager(satCustomerClue.getClueId());
                logger.info("====================结束清空线索渠道信息=======================");
                //根据渠道商代码,找到对应 城市辅导经理/小区经理 列表,推送消息提醒
                List<StaffVO> staffList = staffDubboService.listCityManagerByPartnerCode(partnerCode, null);
                if(staffList != null && staffList.size() > 0){
                    for (StaffVO vo : staffList){
                        SatMessAddVO satMessAddVO = new SatMessAddVO();
                        satMessAddVO.setType("1");
                        satMessAddVO.setTemplateType(1);
                        satMessAddVO.setCustomerName(satCustomerClue.getCustomerName());
                        satMessAddVO.setCustomerNo(satCustomerClue.getCustomerNo());
                        satMessAddVO.setSalesConsultantNo(vo.getAccountNo());
                        satMessAddVO.setReceiveRoleCode("roleCityManager");
                        satMessService.newMess(satMessAddVO);
                    }
                }
            }
        }
    }
}
